package cn.edu.hqu.cst.android.ljc.sqlitetest;

import androidx.appcompat.app.AppCompatActivity;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    EditText etName,etId;
    TextView outInfo;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        etName = findViewById(R.id.name);
        etId = findViewById(R.id.id);
        outInfo = findViewById(R.id.outInfo);
    }

    //查询
    public void query(View view){
        SQLiteOpenHelper helper = MySqliteOpenHelper.getMyInstance(this);
        SQLiteDatabase db = helper.getReadableDatabase();
        if (db.isOpen()){//数据库打开则进入
            Cursor cursor = db.rawQuery("select * from students", null);
            StringBuilder info = new StringBuilder();
            //迭代游标
            while (cursor.moveToNext()){
                int _id = cursor.getInt(0);
                String name = cursor.getString(1);
                //String gender = cursor.getString(2);
                Log.d("myFlag","query: _id:" + _id + " name:" + name);
                info.append("学生id：" + _id + "\t" + "学生姓名：" + name + "\n");
            }
            outInfo.setText(info);
            //关闭游标
            cursor.close();
            //关闭数据库
            db.close();
        }
    }

    //插入
    public void insert(View view) {
        SQLiteOpenHelper helper = MySqliteOpenHelper.getMyInstance(this);
        SQLiteDatabase db = helper.getWritableDatabase();
        if (db.isOpen()){//如果数据库打开
            String name = etName.getText().toString();
            if (!name.isEmpty()){
                //修改语句
                String sql = "insert into students(name) values('"+ name +"')";
                db.execSQL(sql);
            }else {
                Toast.makeText(MainActivity.this,"不要忘记输入姓名",Toast.LENGTH_SHORT).show();
            }


            db.close();
        }
    }

    //修改
    public void update(View view) {
        SQLiteOpenHelper helper = MySqliteOpenHelper.getMyInstance(this);
        SQLiteDatabase db = helper.getWritableDatabase();
        if (db.isOpen()) {//如果数据库打开
            //修改语句
            String sql = "update students set name =? where _id =?";
            String name = etName.getText().toString();
            String _id = etId.getText().toString();
            if (!name.isEmpty() && !_id.isEmpty()){
                int id = Integer.parseInt(_id);
                db.execSQL(sql, new Object[]{name,id});
            }else {
                Toast.makeText(MainActivity.this,"不要忘记输入id和要修改为的姓名",Toast.LENGTH_SHORT).show();
            }
            db.close();
        }
    }

    //删除
    public void delete(View view) {
        SQLiteOpenHelper helper = MySqliteOpenHelper.getMyInstance(this);
        SQLiteDatabase db = helper.getWritableDatabase();
        if (db.isOpen()) {//如果数据库打开
            //删除语句
            String sql = "delete from students where _id =?";
            String _id = etId.getText().toString();
            if (!_id.isEmpty()){
                int id = Integer.parseInt(_id);
                db.execSQL(sql, new Object[]{id});
            }else {
                Toast.makeText(MainActivity.this,"不要忘记输入id",Toast.LENGTH_SHORT).show();
            }
            db.close();
        }
    }
}